package com.nari.ies.ne.nw.server.controller;

import com.nari.ies.ne.nw.server.entity.FileQualityEntity;
import com.nari.ies.ne.nw.server.service.fileQuality.*;
import com.nari.ies.ne.nw.server.util.DateUtil;
import com.nari.ies.ne.nw.server.util.PropUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.util.Date;

@Controller
public class ManualController {

    @Autowired
    private IFileQualityService fileQualityService;
    @Autowired
    private ITJXXFileQualityService TJXXFileQualityService;
    @Autowired
    private IDQYCFileQualityService DQYCFileQualityService;
    @Autowired
    private ICDQYCFileQualityService CDQYCFileQualityService;
    @Autowired
    private IFDJZFileQualityService FDJZFileQualityService;
    @Autowired
    private INBQ_HLXFileQualityService NBQ_HLXFileQualityService;
    @Autowired
    private IQXHJFileQualityService QXHJFileQualityService;
    @Autowired
    private ISYZXXFileQualityService SYZXXFileQualityService;
    @Autowired
    private IXB_FZFileQualityService XB_FZFileQualityService;

    public void TaskDQFileQuality() {
        System.out.println("-----短期开始时间-----"+new Date());
        Date nowDate = null;
        try {
            nowDate = DateUtil.getBeforeOrAfterDate(DateUtil.getToday(), -1);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        String nowTime = DateUtil.SDF.format(nowDate);
        String localPath = PropUtil.getProperties().getProperty("filePath");
        String successpath = PropUtil.getProperties().getProperty("moveSuccessPath");
        String failPath = PropUtil.getProperties().getProperty("moveFailPath");
        String equipmentStatus = PropUtil.getProperties().getProperty("status");
        File dirFile = new File(localPath);
        FileQualityEntity fileQuality ;
        String fileName = null;

        //主备机参数
        int status = Integer.parseInt(equipmentStatus);
//		不判主备机
//		int status = 3;

        System.out.println("短期-主备机状态查询-------------"+status);
        if (dirFile.exists()) {
            if (dirFile.isDirectory()) {
                File[] files = dirFile.listFiles();
                assert files != null;
                for (File file : files) {
                    if (file.isFile()) {
                        try {
                            fileName = file.getName();
                            if(status == 3){
                                System.out.println("准备检测----------------" + fileName);
                                String[] fileNameArr = file.getName().split("_");
                                String fileType = fileNameArr[2];
                                String fileTime = fileNameArr[3];
                                if ("DQYC".equals(fileType) && nowTime.equals(fileTime)) {
                                    System.out.println("文件"+fileName+"进入if判断--");
                                    fileQuality = fileQualityService.checkFileName(file);
                                    if (fileQuality.getMessage().equals("success")) {
                                        fileQuality = DQYCFileQualityService.DQYCFileQuality(file, fileQuality);

                                        if (!fileQuality.getMessage().equals("success")) {
                                            fileQualityService.insertFileQualityData(fileQuality);
                                            PropUtil.moveFile(localPath+ File.separator + fileName, failPath+ File.separator + fileName);
                                        } else {
//										iDataCheckService.DQYCDataCheck(file);
                                            PropUtil.moveFile(localPath+ File.separator + fileName,successpath+ File.separator + fileName);
                                        }
                                    } else {
                                        PropUtil.moveFile(localPath + File.separator + fileName,failPath + File.separator + fileName);
                                    }
                                }
                            } else {
                                PropUtil.moveFile(localPath + File.separator + fileName,failPath + File.separator + fileName);
                            }
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    }


    public void TaskFileQuality() {
        System.out.println("-----开始时间-----"+new Date());
        String localPath = PropUtil.getProperties().getProperty("filePath");
        String successpath = PropUtil.getProperties().getProperty("moveSuccessPath");
        String failPath = PropUtil.getProperties().getProperty("moveFailPath");
//		String equipmentStatus = PropUtil.getProperties().getProperty("status");

        File dirFile = new File(localPath);

        FileQualityEntity fileQuality ;

        String fileName = null;
        //获取主备机状态
//		Integer status = iCurveService.getEquipmentStatus(equipmentStatus);

        //补录数据不判主备机
        Integer status = 3;
        System.out.println("主备机状态查询结果:----------------------"+status);
        if (dirFile.exists()) {

            if (dirFile.isDirectory()) {
                File[] files = dirFile.listFiles();
                assert files != null;
                for (File file : files) {
                    if (file.isFile()) {
                        try {
                            fileName = file.getName();
                            if (status == 3) {
                                System.out.println("准备检测----------------" + fileName);
                                String[] fileNameArr = file.getName().split("_");
                                String fileType = fileNameArr[2];
                                if (fileType.equals("DQYC")) {
                                    continue;
                                }
                                fileQuality = fileQualityService.checkFileName(file);

                                if ("FD".equals(fileQuality.getPlantType())) {
                                    if (fileQuality.getMessage().equals("success")) {
                                        if ("ZTXX-QXHJ".equals(fileQuality.getFileType())) {
                                            fileQuality = QXHJFileQualityService.FdQXHJFileQuality(file, fileQuality);
                                        }
                                        if ("CDQYC".equals(fileQuality.getFileType())) {
                                            fileQuality = CDQYCFileQualityService.CDQYCFileQuality(file, fileQuality);
                                        }
                                        if ("FDJZ".equals(fileQuality.getFileType())) {
                                            fileQuality = FDJZFileQualityService.FDJZFileQuality(file, fileQuality);
                                        }
                                        if ("TJXX".equals(fileQuality.getFileType())) {
                                            fileQuality = TJXXFileQualityService.FDTJXXFileQuality(file, fileQuality);
                                        }
                                        if ("SYZXX".equals(fileQuality.getFileType())) {
                                            fileQuality = SYZXXFileQualityService.FdSYZXXFileQuality(file, fileQuality);
                                        }
                                        if (!fileQuality.getMessage().equals("success")) {
                                            fileQualityService.insertFileQualityData(fileQuality);
                                            PropUtil.moveFile(localPath+ File.separator + fileName, failPath+ File.separator + fileName);
                                        } else {

//											if ("ZTXX-QXHJ".equals(fileQuality.getFileType())) {
//												iDataCheckService.QXHJWindFileQuality(file);
//											}
//											if ("CDQYC".equals(fileQuality.getFileType())) {
//												iDataCheckService.CDQYCDataCheck(file);
//											}
//											if ("FDJZ".equals(fileQuality.getFileType())) {
//												iDataCheckService.windFDJZDataCheck(file);
//											}
//											if ("TJXX".equals(fileQuality.getFileType())) {
//												iDataCheckService.windTJXXDataCheck(file);
//											}
//											if ("SYZXX".equals(fileQuality.getFileType())) {
//												iDataCheckService.SYZXXWindFileQuality(file);
//											}
                                            PropUtil.moveFile(localPath+ File.separator + fileName, successpath+ File.separator + fileName);
                                        }
                                    } else {
                                        fileQualityService.insertFileQualityData(fileQuality);
                                        PropUtil.moveFile(localPath+ File.separator + fileName, failPath+ File.separator + fileName);
                                    }
                                }
                                if ("GF".equals(fileQuality.getPlantType())) {
                                    if (fileQuality.getMessage().equals("success")) {
                                        if ("CDQYC".equals(fileQuality.getFileType())) {
                                            fileQuality = CDQYCFileQualityService.CDQYCFileQuality(file, fileQuality);
                                        }
                                        if ("SYZXX".equals(fileQuality.getFileType())) {
                                            fileQuality = SYZXXFileQualityService.GfSYZXXFileQuality(file, fileQuality);
                                        }
                                        if ("TJXX".equals(fileQuality.getFileType())) {
                                            fileQuality = TJXXFileQualityService.GFTJXXFileQuality(file, fileQuality);
                                        }
                                        if ("ZTXX-QXHJ-TYGZ".equals(fileQuality.getFileType())) {
                                            fileQuality = QXHJFileQualityService.GfQXHJFileQuality(file, fileQuality);
                                        }
                                        if ("NBQ-HLX".equals(fileQuality.getFileType())) {
                                            fileQuality = NBQ_HLXFileQualityService.GfNBQ_HLXFileQuality(file, fileQuality);
                                        }
                                        if ("XB-FZ".equals(fileQuality.getFileType())) {
                                            fileQuality = XB_FZFileQualityService.GfXBFZFileQuality(file, fileQuality);
                                        }
                                        if (!fileQuality.getMessage().equals("success")) {
                                            fileQualityService.insertFileQualityData(fileQuality);
                                            PropUtil.moveFile(localPath+ File.separator + fileName, failPath+ File.separator + fileName);
                                        } else {

//											if ("CDQYC".equals(fileQuality.getFileType())) {
//												iDataCheckService.CDQYCDataCheck(file);
//											}
//											if ("SYZXX".equals(fileQuality.getFileType())) {
//												iDataCheckService.SYZXXPVFileCheck(file);
//											}
//											if ("TJXX".equals(fileQuality.getFileType())) {
//												iDataCheckService.TJXXPVDataCheck(file);
//											}
//											if ("ZTXX-QXHJ-TYGZ".equals(fileQuality.getFileType())) {
//												iDataCheckService.QXHJPVFileQuality(file);
//											}
//											if ("NBQ-HLX".equals(fileQuality.getFileType())) {
//												iDataCheckService.NBQHLXPVDataCheck(file);
//											}
//											if ("XB-FZ".equals(fileQuality.getFileType())) {
//												iDataCheckService.gfXBFZDataCheck(file);
//											}

                                            PropUtil.moveFile(localPath + File.separator + fileName,
                                                    successpath + File.separator + fileName);
                                        }
                                    } else {

                                        PropUtil.moveFile(localPath + File.separator + fileName,
                                                failPath + File.separator + fileName);
                                    }
                                }
                            } else {
                                PropUtil.moveFile(localPath + File.separator + fileName,
                                        failPath + File.separator + fileName);
                            }
                        } catch (IOException e) {
                            e.printStackTrace();
                        }catch (ArrayIndexOutOfBoundsException e){
                            System.out.println("捕获到索引越界异常："+fileName);
                            try {
                                PropUtil.moveFile(localPath + File.separator + fileName,failPath + File.separator + fileName);
                            } catch (IOException e1) {
                                e1.printStackTrace();
                            }
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    }
}
